package com.android.microweather;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class ForecastListActivity extends ListActivity {
	public static final String CLICK_POSITION = "ForecastList_click_position";
	
	private int cityListClickPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.city_forecast_list);
		
		cityListClickPosition = getIntent().getExtras().getInt(CityListActivity.CLICK_POSITION);
		City city = DataModel.getInstance().getCityList().get(cityListClickPosition);
		ForecastListAdapter forecastListAdapter = new ForecastListAdapter(this, city);
		setListAdapter(forecastListAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		Intent i = new Intent(this, DetailsActivity.class);
		i.putExtra(CityListActivity.CLICK_POSITION, cityListClickPosition);
		i.putExtra(ForecastListActivity.CLICK_POSITION, position);
		startActivity(i);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		
		menu.removeItem(R.id.add_city);
		menu.removeItem(R.id.update_all_cities);
		menu.removeItem(R.id.city_management);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		boolean result;
		switch (item.getItemId()) {
			case R.id.settings:
				Intent i = new Intent(this, SettingsActivity.class);
				startActivity(i);
				result = true;
				break;
			default:
				result = super.onMenuItemSelected(featureId, item); 
		}
		return result;
	}
}
